home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / ADVANCED / TEXTEXT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  3.0 KB  |  187 lines

  1.  
  2. /* textext.c - by David Blythe, SGI */
  3.  
  4. /* Example of using texturing for 3D transformable fonts. */
  5.  
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <math.h>
  9. #include <GL/glut.h>
  10. #include "texture.h"
  11. #include "textmap.h"
  12.  
  13. static float scale = .03;
  14. static char *string = "OpenGL rules";
  15. static float transx, transy, rotx, roty;
  16. static int ox = -1, oy = -1;
  17. static int mot = 0;
  18. #define PAN    1
  19. #define ROT    2
  20.  
  21. void
  22. pan(int x, int y)
  23. {
  24.   transx += (x - ox) / 500.;
  25.   transy -= (y - oy) / 500.;
  26.   ox = x;
  27.   oy = y;
  28.   glutPostRedisplay();
  29. }
  30.  
  31. void
  32. rotate(int x, int y)
  33. {
  34.   rotx += x - ox;
  35.   if (rotx > 360.)
  36.     rotx -= 360.;
  37.   else if (rotx < -360.)
  38.     rotx += 360.;
  39.   roty += y - oy;
  40.   if (roty > 360.)
  41.     roty -= 360.;
  42.   else if (roty < -360.)
  43.     roty += 360.;
  44.   ox = x;
  45.   oy = y;
  46.   glutPostRedisplay();
  47. }
  48.  
  49. void
  50. motion(int x, int y)
  51. {
  52.   if (mot == PAN)
  53.     pan(x, y);
  54.   else if (mot == ROT)
  55.     rotate(x, y);
  56. }
  57.  
  58. void
  59. mouse(int button, int state, int x, int y)
  60. {
  61.   if (state == GLUT_DOWN) {
  62.     switch (button) {
  63.     case GLUT_LEFT_BUTTON:
  64.       mot = PAN;
  65.       motion(ox = x, oy = y);
  66.       break;
  67.     case GLUT_MIDDLE_BUTTON:
  68.       mot = ROT;
  69.       motion(ox = x, oy = y);
  70.       break;
  71.     case GLUT_RIGHT_BUTTON:
  72.       break;
  73.     }
  74.   } else if (state == GLUT_UP) {
  75.     mot = 0;
  76.   }
  77. }
  78.  
  79. void 
  80. up(void)
  81. {
  82.   scale += .0025;
  83. }
  84.  
  85. void 
  86. down(void)
  87. {
  88.   scale -= .0025;
  89. }
  90.  
  91. void 
  92. help(void)
  93. {
  94.   printf("Usage: textext [string]\n");
  95.   printf("'h'            - help\n");
  96.   printf("'UP'           - scale up\n");
  97.   printf("'DOWN'         - scale down\n");
  98.   printf("left mouse     - pan\n");
  99.   printf("middle mouse   - rotate\n");
  100. }
  101.  
  102. void 
  103. init(void)
  104. {
  105.   texfntinit("Times-Italic.bw");
  106.   glEnable(GL_TEXTURE_2D);
  107.   glMatrixMode(GL_PROJECTION);
  108.   glLoadIdentity();
  109.   gluPerspective(90., 1., .1, 10.);
  110.   glMatrixMode(GL_MODELVIEW);
  111.   glLoadIdentity();
  112.   glTranslatef(0., 0., -1.5);
  113. }
  114.  
  115. void 
  116. display(void)
  117. {
  118.   float width = texstrwidth(string);
  119.   glClear(GL_COLOR_BUFFER_BIT);
  120.   glPushMatrix();
  121.   glTranslatef(transx, transy, 0.f);
  122.   glRotatef(rotx, 0., 1., 0.);
  123.   glRotatef(roty, 1., 0., 0.);
  124.   glScalef(scale, scale, 0.);
  125.   glTranslatef(-width * 5, 0.f, 0.f);
  126.   texfntstroke(string, 0.f, 0.f);
  127.   glPopMatrix();
  128.   glutSwapBuffers();
  129. }
  130.  
  131. void 
  132. reshape(int w, int h)
  133. {
  134.   glViewport(0, 0, w, h);
  135. }
  136.  
  137. /* ARGSUSED1 */
  138. void
  139. key(unsigned char key, int x, int y)
  140. {
  141.   switch (key) {
  142.   case 'h':
  143.     help();
  144.     break;
  145.   case '\033':
  146.     exit(1);
  147.     break;
  148.   default:
  149.     break;
  150.   }
  151. }
  152.  
  153. /* ARGSUSED1 */
  154. void
  155. special(int key, int x, int y)
  156. {
  157.   switch (key) {
  158.   case GLUT_KEY_UP:
  159.     up();
  160.     break;
  161.   case GLUT_KEY_DOWN:
  162.     down();
  163.     break;
  164.   }
  165.   glutPostRedisplay();
  166. }
  167.  
  168. int 
  169. main(int argc, char **argv)
  170. {
  171.   if (argc > 1)
  172.     string = argv[1];
  173.   glutInit(&argc, argv);
  174.   glutInitWindowSize(512, 512);
  175.   glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
  176.   (void) glutCreateWindow("textext");
  177.   init();
  178.   glutDisplayFunc(display);
  179.   glutKeyboardFunc(key);
  180.   glutSpecialFunc(special);
  181.   glutReshapeFunc(reshape);
  182.   glutMouseFunc(mouse);
  183.   glutMotionFunc(motion);
  184.   glutMainLoop();
  185.   return 0;             /* ANSI C requires main to return int. */
  186. }
  187.